/*
 * $Id: ScriptAlgo.java,v 1.2 2002/01/25 13:49:06 psturc Exp $
 * $Log: ScriptAlgo.java,v $
 * Revision 1.2  2002/01/25 13:49:06  psturc
 * setKey method added
 *
 *
 * Created on December 14, 2000, 8:40 PM
 */

package com.gemalto.Kryptik.Algo;


//package com.gemalto.Kryptik;
import bsh.*;
import java.io.File;
import java.io.IOException;
import com.gemalto.Kryptik.Gui.*;
import com.gemalto.Kryptik.*;
import javax.swing.*;


/**
 * 
 * @author  STURC
 * @version 
 */
public class ScriptAlgo extends AbstractAlgo implements ISymetricCipher, ISignature {

	  private String scriptName;
	  private Interpreter i;
	  private String[] methodList;


	 public class ScriptPanel extends JPanel implements AlgoPanel {
		
			public ScriptPanel()  throws AlgoException  {
				try {
			        	i.set("panel", this);
					i.eval("initGui(panel);");
				} catch (bsh.EvalError e) { 
					e.printStackTrace();
					throw new AlgoException("Script evaluation failed!\n"+e.getMessage());
				}
			}


			public void initGui(){
			}

			public void update(){
			}
	}






	  public ScriptAlgo(String algoName, String shortName, String scriptName)  throws AlgoException {
		this.algoName  = algoName;
		this.shortName = shortName;
		this.scriptName= scriptName;		

		// Deferred loading ...
		//this.i = null;
		this.i=Beanshell.newInterpreter();
		try {
			i.source(scriptName);
        	} catch ( IOException e ){
                	throw new AlgoException("Script file `"+scriptName+"' not found.");
	        } catch (bsh.EvalError e) { 
			e.printStackTrace();
			throw new AlgoException("Script evaluation failed!\n"+e.getMessage());
		}
		methodList = i.getNameSpace().getMethodNames();

	
		if(hasMethod("initGui")){
			panel=new ScriptPanel();
		} else {
			panel=new DummyAlgoPanel();
		}
	  }


	  private boolean hasMethod(String name){
		boolean found=false;
		for (int j=0;j<methodList.length;j++){
			if (methodList[j].equals(name)) found=true;
		}
		return found;
	  }


	
	  public void activate() throws AlgoException {
                try {
			if (hasMethod("activate")){
				i.eval("activate();");
			}
		} catch (bsh.EvalError e) { 
			e.printStackTrace();
			throw new AlgoException("Script evaluation failed!\n"+e.getMessage());
		}
	  }


	  public void deactivate() throws AlgoException {
                try {
			if (hasMethod("deactivate")){
				 i.eval("deactivate();");
			}
		} catch (bsh.EvalError e) { 
			e.printStackTrace();
			throw new AlgoException("Script evaluation failed!\n"+e.getMessage());
		}
	  }



	  public  String doEncryption(String plainText) throws AlgoException {
		Object result=null;
		try {    
			if (plainText.length()%2!=0) throw new AlgoException("Incorrect length of the plain text!");
			result=i.eval("doEncryption(\""+plainText+"\");");
		} catch (bsh.EvalError e) { 
			e.printStackTrace();
			throw new AlgoException("Script evaluation failed!\n"+e.getMessage());
		}
		return (String)result;
	  }


	  public String doDecryption(String encryptedText) throws AlgoException {
		Object result=null;
		try {    
			if (encryptedText.length()%2!=0) throw new AlgoException("Incorrect length of the encrypted text!");
			result=i.eval("doDecryption(\""+encryptedText+"\");");
		} catch (bsh.EvalError e) { 
			e.printStackTrace();
			throw new AlgoException("Script evaluation failed!\n"+e.getMessage());
		}
		return (String)result;
	  }


	  public String doSignature(String plainText) throws AlgoException {
		return "";
	  }

	  
	public void setKey(String key) throws AlgoException {
		try {
			if (hasMethod("setKey"))
				i.eval("setKey(\""+key+"\");");
		} catch (bsh.EvalError e){
			e.printStackTrace();
			throw new AlgoException("Script evaluation failed!\n"+e.getMessage());
		}
	}



}
